﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ULSDeobfuscator {

    public class SettingsSaver {

        private const string SettingsFile  = "ULSObfuscatorSettings.xml";

        public static ProgramSettings LoadSettings() {

            try {
                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain();

                if (isf.GetFileNames(SettingsFile).Length == 0) {
                    return new ProgramSettings();
                }

                using (
                    IsolatedStorageFileStream settingsStream = new IsolatedStorageFileStream(SettingsFile,
                        FileMode.OpenOrCreate, FileAccess.Read, isf)) {

                    XmlSerializer ser = new XmlSerializer(typeof (ProgramSettings));
                    return (ProgramSettings) ser.Deserialize(settingsStream);
                }
            } catch (Exception ex) {

                App.DebugLog("Error while loading settings: " + ex);
                return new ProgramSettings();
            }
        }

        public static void SaveSettings(ProgramSettings settings) {

            try {
                using (
                    IsolatedStorageFileStream settingsStream = new IsolatedStorageFileStream(SettingsFile,
                        FileMode.Create, FileAccess.Write)) {

                    XmlSerializer ser = new XmlSerializer(typeof (ProgramSettings));
                    ser.Serialize(settingsStream, settings);
                }
            } catch (Exception ex) {
                App.DebugLog("Error while saving settings: " + ex);
            }
        }
    }
}
